(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ReservationCtrl
     * @description
     * # ReservationCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ReservationCtrl', ReservationCtrl);

    ReservationCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function ReservationCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);

        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                query: { store_id: $rootScope.store_id },
            };
            if ($scope.reservation_id) {
                query.query.reservation_id = $scope.reservation_id;
            }

            dataService.reservationGet(query).then(function(re1) {
                if (re1.reservations) {
                    if ($scope.reservation_id) {
                        $scope.reservation = re1.reservations[0];
                    } else {
                        $scope.reservations = re1.reservations;
                        $scope.storeTotal = re1.total_count;
                    }
                }
            });
        }


        //添加数据 初始化
        $scope.infoAction = function() {
            $scope.reservation_id = $location.search().reservation_id;
            if ($scope.reservation_id) {
                $scope.getlist();
            }

        }

        //生成数据
        $scope.stltodat = function(id, $event) {
            if (id) {
                var query = { "schema_id": id };
                dataService.stltodataAdd(query).then(function(re1) {
                    helper.myTip("success", $event.currentTarget, null, 3);
                    $scope.getlist(1);
                });
            }

        }

        // schema_type_id
        $scope.saveData = function() {

            var zhuoziApiAry = {
                "add": "schemaAdd",
                "up": "schemaUpdate",
            }
            var api = "add";
            if ($scope.schema_id) {
                api = "up";
            }

            dataService[zhuoziApiAry[api]]($scope.schema).then(function(r) {
                helper.myAlert("success", null, null, function() {
                    $state.go("schema.list");
                });
            })
        }
    }
})();